﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleVisio.Util;
using SimpleVisio.Model;
using System.Xml;
using System.Xml.Linq;
using static SimpleVisio.Util.CoOrdinates;
using System.Drawing;

namespace SimpleVisio
{
    class SimpleVXMLreader
    {
        private AppConfigReader appconf;
        public List<Container> visioContainers = new List<Container>();

        public SimpleVXMLreader(AppConfigReader appconf)
        {
            this.appconf = appconf;
            string xmlpath = @appconf.InputXML;// @"C:\Manoj\SimpleVisio\SimpleVisio\SimpleVisio\input.xml";
            var xmlDoc = XDocument.Load(xmlpath);
            //var xDoc = XDocument.Load(configurationPAthFileTextbox.Text);
            var containers =
                     from container in xmlDoc.Descendants("container")
                         //where container.Attribute("ID").Value != "0"
                    select new Container
                     {
                         id = Convert.ToInt32(container.Element("id").Value),
                         name = container.Element("name").Value,
                         enabled = Convert.ToBoolean(container.Element("enable").Value),
                         components = new List<Component>(
                             from component in container.Descendants("component")
                             select new Component
                             {
                                 id = Convert.ToInt32(component.Element("id").Value),
                                 name = component.Element("name").Value,
                                 type = component.Element("type").Value,
                                 myPoint = new Point(Convert.ToInt32(component.Element("x").Value), Convert.ToInt32(component.Element("y").Value)),//component.Element("name").Value,
                                 connectors = new List<Connector>(
                                     from connector in component.Descendants("connector")
                                     select new Connector
                                     {
                                         //id = Convert.ToInt32(component.Element("id").Value),
                                         id = Convert.ToInt32(connector.Element("id").Value),
                                         name = connector.Element("name").Value,
                                         source = connector.Element("id").Value,
                                         destination = component.Element("id").Value
                                     })
                             }),
                         connectors = new List<Connector>(
                                from connector in container.Descendants("containerconnector")
                                select new Connector
                                {
                                    //id = Convert.ToInt32(container.Element("id").Value),
                                    id = Convert.ToInt32(connector.Element("id").Value),
                                    name = connector.Element("name").Value,
                                    source = connector.Element("id").Value,
                                    destination = container.Element("id").Value
                                } )
                     };

            foreach (var container in containers)
            {
                visioContainers.Add(container);
            }
        

        }

        
    }
}
